package top.xmj.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.xmj.dao.FriendMapper;
import top.xmj.domain.FriendBO;
import top.xmj.exception.BusinessException;
import top.xmj.pojo.Friend;
import top.xmj.service.IFriendService;

import java.util.List;

/**
 * @author 冰咖啡
 * Date:2022/12/13 19:28
 * Description:
 */
@Service
public class FriendServiceImpl implements IFriendService {

    @Autowired
    private FriendMapper friendMapper;

    @Override
    public List<FriendBO> getListByUserId(String userId) {
        return friendMapper.getListByUserId(userId);
    }

    @Override
    public boolean addFriend(String userId, String friendId, String applyMsg) {
        boolean f = isFriend(userId, friendId);
        if(f){
            throw new BusinessException(405,"该用户已经是您的好友，无需添加",null);
        }
        Friend friend1 = new Friend(userId, friendId, applyMsg,3);
        int res1 = friendMapper.insert(friend1);
        Friend friend2 = new Friend(friendId, userId, applyMsg,4);
        int res2 = friendMapper.insert(friend2);
        return res1 == 1 && res2 == 1;
    }

    @Override
    public boolean agreeAddFriend(String userId, String friendId) {
        return friendMapper.agreeAddFriend(userId, friendId) > 0;
    }

    @Override
    public boolean deleteFriend(String userId, String friendId) {
        return friendMapper.deleteFriend(userId, friendId) > 0;
    }

    @Override
    public boolean rejectAddFriend(String userId, String friendId) {
        return friendMapper.rejectAddFriend(userId, friendId) > 0;
    }

    @Override
    public FriendBO findFriendByUserName(String username) {
        FriendBO friend = friendMapper.findFriendByUserName(username);
        if(friend == null){
            throw new BusinessException(404,"没有找到该用户",null);
        }
        friend.setStatus(8);
        return friend;
    }

    @Override
    public boolean isFriend(String userId,String friendId){
        Integer res = friendMapper.isFriend(userId, friendId);
        System.out.println("res===>"+res);
        return res == 1 ? true : false;
    }
}
